package com.erplan.studentmgt.desktop.commons;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;

import com.erplan.studentmgt.desktop.StudentMgt;

public class GeneralFunctions {
	public static void setShellInitSettings() {
		final Image iconImage = new Image(StudentMgt.display, StudentMgt.class.getResourceAsStream("images/app_icon.png"));
		StudentMgt.shell.setImage(iconImage);
		StudentMgt.shell.setText(StudentMgt.toolTextSmall);

		StudentMgt.shell.addListener (SWT.Close, new Listener () {
			public void handleEvent (Event event) {
				MessageBox messageBox = new MessageBox (StudentMgt.shell, SWT.APPLICATION_MODAL | SWT.ICON_QUESTION | SWT.YES | SWT.NO);
				messageBox.setText(StudentMgt.toolTextSmall);
				messageBox.setMessage("Close " + StudentMgt.toolTextSmall + " ?");
				event.doit = messageBox.open() == SWT.YES;
			}
		});
	}

	public static void setShellLayout() {
		GridLayout gridLayout = new GridLayout();
		gridLayout.verticalSpacing = 0;
		gridLayout.marginWidth = 0;
		gridLayout.marginHeight = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.marginTop = 3;
		StudentMgt.shell.setLayout(gridLayout);
	}

	public static void setShellLocation() {
		Rectangle shellRect = StudentMgt.shell.getBounds();
		Rectangle displayRect = StudentMgt.display.getBounds();
		int shellX = (displayRect.width - shellRect.width) / 2;
		int shellY = (displayRect.height - shellRect.height) / 2;
		StudentMgt.shell.setLocation(shellX, shellY);
	}

	public static void setShellSizeAndLocation(int shellWidth, int shellHeight) {
		StudentMgt.shell.setSize(shellWidth, shellHeight);
		Rectangle shellRect = StudentMgt.shell.getBounds();
		Rectangle displayRect = StudentMgt.display.getBounds();
		int shellX = (displayRect.width - shellRect.width) / 2;
		int shellY = (displayRect.height - shellRect.height) / 2;
		StudentMgt.shell.setLocation(shellX, shellY);
	}
}